<style lang="stylus" scoped>

</style>

<template>
  <q-scroll-area ref="scrollChat" class="fit q-px-sm bg-grey-3">
    <div>
      <q-chat-message
        v-for="item in list"
        :key="item.id"
        :name="item.userType === '1'? '胡世洋': item.userType === '2'? '尤敏明': item.userType === '3'? '郭战旗': item.userType === '4'? '谌啸林': item.userType === '5'? '刘虎木': item.userType === '6'? '胡扬东': ''"
        :avatar="item.userType === 1? 'https://cdn.quasar.dev/img/avatar4.jpg': 'https://cdn.quasar.dev/img/boy-avatar.png'"
        :text="[item.viewName + '：' + item.content]"
        :sent="item.userType === '1'? true: false"
        :stamp="(item.type === 1? '问题': item.type === 2? '沟通': '优化') + ':' + item.createTime"
      />
    </div>
  </q-scroll-area>
</template>

<script>
import { wpLeftNav, ppLeftNav } from 'src/plugins/leftNav'
export default {
  props: {
    flag: Boolean,
    moduleName: {
      type: String,
      required: true
    },
    routerChange: Boolean
  },
  data () {
    return {
      list: [],
      codeForChat: ''
    }
  },
  computed: {
    navList () {
      let arr = []
      if (this.moduleName === 'pp') {
        arr = [...ppLeftNav]
      }
      if (this.moduleName === 'wp') {
        arr = [...wpLeftNav]
      }
      return arr
    }
  },
  watch: {
    flag () {
      console.log(this.flag, 'flag---changed')
      this.getMessage({ page: 1 })
    },
    routerChange () {
      this.getMessage({ page: 1 })
    }
  },
  mounted () {
    this.getMessage({ page: 1 })
  },
  methods: {
    getMessage ({ page }) {
      this.getMenuCode(this.navList)
      this.$api({
        method: 'post',
        url: '/mdm/systeminfo/query',
        params: {
          pageNo: page,
          pageSize: 0,
          sort: 'createTime',
          order: 'asc',
          viewCode: this.codeForChat
        },
        emulateJSON: true,
        successCallback: ({ data }) => {
          // data.list.map(item => { this.list.push(item) })
          this.list = data.list
          setTimeout(() => {
            this.toBottom()
          }, 500)
        },
        errorCallback (err) {
          console.log('未处理的业务级error：', err)
        }
      })
    },
    toBottom () {
      this.$refs['scrollChat'].setScrollPosition(this.$refs['scrollChat'].scrollSize, 500)
    },
    getMenuCode (list) {
      for (let index = 0; index < list.length; index++) {
        const menu = list[index]
        if (menu.children) {
          this.getMenuCode(menu.children)
        } else {
          if (menu.fullPath === this.$route.meta.fullPath) {
            this.codeForChat = menu.codeForChat
            break
          }
          continue
        }
      }
    }
  }
}
</script>
